//
//  DisTypePopColCell.swift
//  WalkNovel
//
//  Created by Kevin on 2023/7/11.
//

import UIKit

class DisTypePopColCell: UICollectionViewCell, UIScrollViewDelegate {
    
    lazy var scrollView: UIScrollView = {
        let scro = UIScrollView.init(frame: .zero)
        scro.showsHorizontalScrollIndicator = true
        scro.showsVerticalScrollIndicator = false
        scro.delegate = self
        return scro
    }()
    
    var popViewArr = [DisTypePopView]()
    
    var popArr: [BookDetailModel]? {
        didSet {
            if let arr = popArr, arr.count != 0 {
                for item in scrollView.subviews {
                    item.removeFromSuperview()
                }
                popViewArr.removeAll()
                let showPopArr = arr.count > 6 ? arr.sample(size: 6) : arr
                for i in 0..<(showPopArr?.count ?? 0) {
                    let view = DisTypePopView()
                    view.frame = CGRect(x: CGFloat(i)*(scaleWidth(80)+200+12+12)+12, y: 0, width: (scaleWidth(80)+200+12), height: scaleWidth(116)+12)
                    view.model = showPopArr?[i]
                    popViewArr.append(view)
                    scrollView.addSubview(view)
                }
                scrollView.contentSize = CGSize(width: CGFloat(showPopArr?.count ?? 0)*(scaleWidth(80)+200+12+12)+12, height: 0)
            }
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        addSubview(scrollView)
        scrollView.snp.makeConstraints { (make) in
            make.top.equalTo(12)
            make.left.bottom.right.equalTo(0)
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func scrollViewDidScroll(_ scrollView: UIScrollView) {   
        (scrollView.subviews[(scrollView.subviews.count - 1)].subviews[0]).backgroundColor = .init(named: "red_FE95B4")
    }
}

class DisTypePopView: UIView {
    
    lazy var bookImage: UIImageView = {
        let image = UIImageView(image: UIImage(named: "placeholder_cover_ver"))
        image.contentMode = .scaleAspectFill
        image.wn_cornerWithBorder(2)
        image.clipsToBounds = true
        return image
    }()
    
    lazy var disLabel: UILabel = {
        let label = UILabel()
        label.font = .wn_font(name: .bold, size: 9)
        label.textColor = .white
        label.backgroundColor = .init(named: "red_FE95B4")
        label.textAlignment = .center
        label.isHidden = true
        return label
    }()
    
    lazy var nameLabel: UILabel = {
        let label = UILabel()
        label.font = .wn_font(name: .bold, size: 14)
        label.textColor = .black
        return label
    }()
    
    lazy var authorLabel: UILabel = {
        let label = UILabel()
        label.font = .wn_font(name: .bold, size: 11)
        label.textColor = .init(named: "gray_666666")
        return label
    }()
    
    lazy var desLabel: UILabel = {
        let label = UILabel()
        label.font = .wn_font(name: .bold, size: 11)
        label.textColor = .init(named: "gray_999999")
        label.numberOfLines = 5
        return label
    }()
    
    lazy var countBtn: UIButton = {
        let btn = UIButton(type: .custom)
        btn.setTitleColor(.black, for: .normal)
        btn.titleLabel?.font = .wn_font(name: .bold, size: 10)
        btn.setImage(UIImage(named: "discover_hot"), for: .normal)
        btn.imageView?.contentMode = .scaleAspectFit
        return btn
    }()
    
    lazy var bookBtn: UIButton = {
        let btn = UIButton(type: .custom)
        btn.rx.tap.bind { [weak self] in
            self?.viewContainingController?.wn_pushBookVC(self?.model?.bookId ?? "")
        }.disposed(by: rx.disposeBag)
        return btn
    }()
    
    var model: BookDetailModel? {
        didSet {
            if let m = model {
                bookImage.wn_setupImg(m.coverUrl, placeHolder: "placeholder_cover_ver")
                nameLabel.text = m.name
                authorLabel.text = m.authorname
                desLabel.text = m.des
                countBtn.setTitle("\(m.readings)".wn_wan(), for: .normal)
            }
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = .init(named: "white_F7F7F7")
        addSubview(bookImage)
        bookImage.addSubview(disLabel)
        addSubview(nameLabel)
        addSubview(authorLabel)
        addSubview(countBtn)
        addSubview(desLabel)
        addSubview(bookBtn)
        bookImage.snp.makeConstraints { (make) in
            make.top.equalTo(6)
            make.left.equalTo(6)
            make.size.equalTo(CGSize(width: scaleWidth(80), height: scaleWidth(116)))
        }
        disLabel.snp.makeConstraints { (make) in
            make.top.equalTo(5)
            make.right.equalTo(-5)
            make.height.equalTo(14)
            make.width.equalTo(0)
        }
        nameLabel.snp.makeConstraints { (make) in
            make.top.equalTo(bookImage.snp.top)
            make.left.equalTo(bookImage.snp.right).offset(8)
            make.right.equalTo(-6)
        }
        authorLabel.snp.makeConstraints { (make) in
            make.top.equalTo(nameLabel.snp.bottom).offset(4)
            make.left.equalTo(nameLabel.snp.left)
            make.right.equalTo(-6)
        }
        countBtn.snp.makeConstraints { (make) in
            make.centerY.equalTo(authorLabel.snp.centerY)
            make.right.equalTo(-6)
            make.height.equalTo(10)
        }
        desLabel.snp.makeConstraints { (make) in
            make.left.equalTo(nameLabel.snp.left)
            make.top.equalTo(authorLabel.snp.bottom).offset(4)
            make.right.equalTo(-6)
        }
        bookBtn.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        countBtn.layoutButton(style: .left, imageTitleSpace: 2)
        wn_cornerWithBorder(4)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
